package java_test;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class TestTime {
	
	public static String randomDateBetweenMinAndMax(){ 
	    Calendar calendar = Calendar.getInstance(); 
	    //注意月份要减去1 
	    calendar.set(2018,1,1); 
	    calendar.getTime().getTime(); 
	    //根据需求，这里要将时分秒设置为0 
	    calendar.set(Calendar.HOUR_OF_DAY, 0); 
	    calendar.set(Calendar.MINUTE, 0); 
	    calendar.set(Calendar.SECOND,0); 
	    long min = calendar.getTime().getTime();; 
	    calendar.set(2018,2,31); 
	    calendar.set(Calendar.HOUR_OF_DAY,0); 
	    calendar.set(Calendar.MINUTE,0); 
	    calendar.set(Calendar.SECOND,0); 
	    calendar.getTime().getTime(); 
	    long max = calendar.getTime().getTime(); 
	    //得到大于等于min小于max的double值 
	    double randomDate = Math.random()*(max-min)+min; 
	    //将double值舍入为整数，转化成long类型 
	    calendar.setTimeInMillis(Math.round(randomDate)); 
	    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        df.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        System.out.println(calendar.getTime().getHours());
        return df.format(calendar.getTime());
//	    return calendar.getTime().toString(); 
	  }
	
	public static void main(String[] args) {
		String str = TestTime.randomDateBetweenMinAndMax();
		System.out.println(str);
	}
}

